var prefix = ctx + "manage/userAgent"

$(function () {
    var columns = [{
        checkbox: true
    },
        {
            field: 'plUserId',
            title: '用户ID'
        },
        {
            field: 'kickback',
            title: '佣金比例',
            formatter: function (value, row, index) {
                return value + "%";
            }

        },
        {
            field: 'status',
            title: '帐号状态',
            formatter: function (value, row, index) {
                if (value === 0) {
                    return "   <div class=\"onoffswitch\">\n" +
                        "      <input type=\"checkbox\" onchange=\"checkStatus("+row.userId+")\" class=\"onoffswitch-checkbox btn-embossed btn\" id=\"status"+row.userId+"\" name=\"status"+row.userId+"\" checked=\"checked\">\n" +
                        "      <label class=\"onoffswitch-label\" for=\"status"+row.userId+"\"> <span class=\"onoffswitch-inner\"></span>\n" +
                        "          <span class=\"onoffswitch-switch\"></span>\n" +
                        "      </label>\n" +
                        "  </div>";
                }
                if (value === 1) {
                    return "   <div class=\"onoffswitch\">\n" +
                        "      <input type=\"checkbox\" onchange=\"checkStatus("+row.userId+")\" class=\"onoffswitch-checkbox btn-embossed btn\" id=\"status"+row.userId+"\" name=\"status"+row.userId+"\">\n" +
                        "      <label class=\"onoffswitch-label\" for=\"status"+row.userId+"\"> <span class=\"onoffswitch-inner\"></span>\n" +
                        "          <span class=\"onoffswitch-switch\"></span>\n" +
                        "      </label>\n" +
                        "  </div>";
                }

            }
        },
        {
            field: 'loginName',
            title: '登录账号'
        },
        {
            field: 'userName',
            title: '用户昵称'
        },
        {
            field: 'phonenumber',
            title: '手机号码'
        },
        {
            field: 'loginIp',
            title: '最后登陆IP'
        },
        {
            field: 'loginDate',
            title: '最后登陆时间'
        },
        {
            field: 'createBy',
            title: '创建者'
        },
        {
            field: 'createDateTimeStr',
            title: '创建时间'
        },
        {
            title: '操作',
            align: 'center',
            formatter: function (value, row, index) {
                var actions = [];
                actions.push('<a class="btn btn-success btn-xs ' + resetPwdFlag + '" href="#" onclick="edit(\'' + row.userId + '\')"><i class="fa fa-edit"></i>编辑</a>');
                actions.push('<a style="margin-left: 10px" class="btn btn-info btn-xs ' + resetPwdFlag + '" href="#" onclick="resetPwd(\'' + row.userId + '\')"><i class="fa fa-key"></i>重置</a>');
                return actions.join('');
            }
        }];
    var url = prefix + "/list";
    $.initTable(columns, url);
});
/*用户管理-重置密码*/
function resetPwd(userId) {
    var url = prefix + '/resetPwd/' + userId;
    layer_show("重置密码", url, '800', '300');
}
/*用户-新增*/
function add() {
    var url = prefix + '/add';
    layer_showAuto("新增代理", url);
}

/*用户-修改*/
function edit(userId) {
    var url = prefix + '/edit/' + userId;
    layer_showAuto("修改代理", url);
}

function checkStatus(id) {
    var checkStatus = $("#status"+id);
    var plStatus;
    if (checkStatus.is(":checked")) {
        plStatus = 0;
    } else {
        plStatus  = 1;
    }
    _ajax_query(ctx + "manage/userAgent/status", {userId:id,status:plStatus}, function (result) {
        if (result.code == web_status.SUCCESS) {
            $.modalMsg(result.msg, modal_status.SUCCESS);
        } else {
            $.modalAlert(result.msg, modal_status.FAIL);
        }
    });
}
